package com.xxg.imagemanager.controller;

import com.xxg.imagemanager.domain.query.CheckQuery;
import com.xxg.imagemanager.domain.result.BaseResult;
import com.xxg.imagemanager.domain.result.ReturnCodeEnum;
import com.xxg.imagemanager.service.ImageManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 图片管理接口
 *
 * @author xuxiaogang
 * @date 2021-12-09
 */
@Api(tags = "图片管理接口")
@RestController
@RequestMapping("/image")
public class ImageManagerController {
    private final ImageManagerService imageManagerService;

    public ImageManagerController(ImageManagerService imageManagerService) {
        this.imageManagerService = imageManagerService;
    }

    @ApiOperation("批量上传图片到图床")
    @RequestMapping(value = "upload", method = RequestMethod.GET)
    public BaseResult<String> upload(HttpServletRequest request) {
        imageManagerService.batchUploadImage();
        return new BaseResult<>("ok");
    }

    @ApiOperation("获取图床中文件数量")
    @RequestMapping(value = "count", method = RequestMethod.GET)
    public BaseResult<String> count(HttpServletRequest request) {
        return imageManagerService.count();
    }

    @ApiOperation("构建商品图片索引库")
    @RequestMapping(value = "createImageIndex", method = RequestMethod.GET)
    public BaseResult<String> createImageIndex(HttpServletRequest request) {
        imageManagerService.batchCreateImageIndex();
        return new BaseResult<>("ok");
    }

    @ApiOperation("以图搜图")
    @RequestMapping(value = "searchByPic", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "file", paramType = "form", value = "图片文件", required = true, dataType = "file")})
    public BaseResult<String> imageSearch(@RequestPart MultipartFile file, HttpServletRequest request) {
        long size = 4194304;
        if (file.getSize() > size) {
            return new BaseResult<>(ReturnCodeEnum.INVALID_PARAMETER, "图片大小不能超过4MB", null);
        }
        // 检查是否时图片
        BufferedImage bi;
        byte[] image;
        try {
            bi = ImageIO.read(file.getInputStream());
            image = file.getBytes();
        } catch (IOException e) {
            return new BaseResult<>(ReturnCodeEnum.INVALID_PARAMETER, e.getMessage(), null);
        }
        if (bi == null) {
            return new BaseResult<>(ReturnCodeEnum.INVALID_PARAMETER, "上传的文件不是图片", null);
        }
        return imageManagerService.searchByPic(image);
    }

    @ApiOperation("检查图片是否存在")
    @RequestMapping(value = "check", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "checkQuery", value = "查询条件", required = true, dataType = "CheckQuery")})
    public BaseResult<String> check(@RequestBody @Validated CheckQuery checkQuery, HttpServletRequest request) {
        return imageManagerService.check(checkQuery.getFileName());
    }
}
